<?php
/*
 * 服务中心
 */

namespace App\Http\Controllers\Property;


use App\Common\json;
use App\Common\SmsClient;
use App\Http\Controllers\Controller;
use App\Libraries\upload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ServiceController extends  Controller{


    //服务中心常用信息
    public function SelectService(Request $request)
    {
        if ($request->isMethod('post')) {
            $arr    = [];
            $village_id   = $request ->input('village_id'); //小区ID
            $data = DB::table('service')
                ->select('service_name', 'service_phone', 'service_type')
                ->leftjoin('servicetype', 'service.servicetype_id', '=', 'servicetype.service_type_id')
                ->where('service_village',$village_id)
                ->get();
           // $tmpArray = array();
            foreach ($data as $row) {
                        $key = $row ->service_type;
                        $tmpArray[$key]['service_type'] =$key;
                        if(empty($row ->service_name)){
                          $service_name  = "";
                        }else{
                            $service_name  = $row ->service_name;
                        }
                if(empty($row ->service_phone)){
                    $service_phone  = "";
                }else{
                    $service_phone  = $row ->service_phone;
                }
                        $tmpArray[$key]['service'][] =array('name' =>$service_name,'phone' =>$service_phone);
                        $da = array_values($tmpArray);
                        if(empty($da)){
                            $dp   = array();
                        }else{
                            $dp   = $da;
                        }
            }
             if($data == true){
                 return json::json('success', '获取常用电话数据成功', $dp);
             }else{
                 return json::json('failure','由于网络或者不正当原因失败',$arr);
             }
         }else{
             return json::json('101','提交方式错误','');
         }


    }

    //获取服务人员
    public function SelectServiceReview(Request $request){
        if($request ->isMethod('post')){
            $arr          = [];
            $village_id   = $request ->input('village_id'); //小区ID
            $data         = DB::table('hashslinger')
                          ->select('hashslinger_id','name','hashslinger_phone','position','hashslinger_head')
                          ->where('village_hashslinger',$village_id)
                          ->get();
            foreach ($data as $value){
                $dd[]       =array(
                    'hashslinger_id' =>$value->hashslinger_id,
                    'name'            =>$value->name,
                    'phone'           =>$value->hashslinger_phone,
                    'position'       =>$value->position,
                    'hashslinger_head'  => $value->hashslinger_head
                );
                if(empty($dd)){
                    $ds  = array();
                }else{
                    $ds  = $dd;
                }
            }
            if($data == true){
                return json::json('success', '获取本小区服务人员成功', $ds);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',$arr);
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

    //点评
    public function RemarkOn(Request $request,$type){
        if($request ->isMethod('post')){
            $arr   = (object)array();
            if($type ==1){
                //好评管理
                $hashslinger_id  = $request ->input('hashslinger_id');
                $user_id         = $request ->input('user_id');
                $comment         = $request ->input('comment');
                $star            = $request ->input('star');

                $data = DB::table('servicereview')
                    ->insert([
                        'user_servicereview' => $user_id,
                        'hashslinger_servicereview' => $hashslinger_id,
                        'comment' => $comment,
                        'well'    => $star,
                    ]);
                if ($data == true) {
                    return json::json('success', '对小区服务人员服务点评成功', $arr);
                } else {
                    return json::json('failure', '由于网络或者不正当原因失败', $arr);
                }
            }else{
                //差评管理
                $hashslinger_id  = $request ->input('hashslinger_id');
                $user_id         = $request ->input('user_id');
                $comment         = $request ->input('comment');
                $star            = $request ->input('star');

                $data = DB::table('servicereview')
                    ->insert([
                        'user_servicereview' => $user_id,
                        'hashslinger_servicereview' => $hashslinger_id,
                        'comment' => $comment,
                        'bad'     => $star,
                    ]);
                if ($data == true) {
                    return json::json('success', '对小区服务人员服务点评成功', $arr);
                } else {
                    return json::json('failure', '由于网络或者不正当原因失败', $arr);

                }
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }




}